package com.ruoyi.mymod.utils;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.concurrent.CompletableFuture;

public abstract  class ProgExecContr {

    private static Object lock=new Object();

    public static boolean checkIsRun(String pid){
        boolean b=true;
        synchronized (lock) {
            /*检查数据库program记录是否锁定
            if isRun  b= true
            else
            改变数据库记变为运行状态
            return b=f
            endif
             */
            Dbutil.R r = null;
            Dbutil.Qp qp = null;
            try {
                qp=new Dbutil.Qp();
                Object o = qp.setUsrpwd("psi", "psi").setSqlStr("select t.isrun from PEC_PROGRAM t where t" +
                        ".program='"+pid+"'").select();
                r = (Dbutil.R) o;
                if (r.body.length>0 && r.body[0]!=null && r.body[0][0]!=null){

                    if (r.body[0][0].equals("1")){

                    }else{
                        qp.setSqlStr("update PEC_PROGRAM t set t.isrun=1 where t.program='"+pid+"'").select();
                        b=false;
                    }

                }else{
                    b=false;
                }

            }finally {
                qp.closeConn();
            }

            return b;
        }
    }

    //获取程序
    public static ArrayList getec(){


        return null;
    }

    //执行
    public String Process(String programid){

        Dbutil.R r = null;
        final Dbutil.Qp[] qp = {null};
        try {
            if (checkIsRun(programid))  return "";
            if (true){  //isAsyn
                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                Date tdates = new Date();
                String tdate = sdf.format(tdates);
                final String[] result = {""};
                final String[] edate = {""};
                CompletableFuture.runAsync(() -> {
                    // 新线程任务
                    try {
                        qp[0] = new Dbutil.Qp();
                        Object o = qp[0].setUsrpwd("pigpos", "pigpos").func(1,"wx_chk.f");
                        result[0]=o.toString();
                        Date edates = new Date();
                        edate[0] = sdf.format(edates);
                        afterProcess();
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }).thenRun(() -> {
                    qp[0].setSqlStr("insert into PEC_PROGRAM m  (id,code,name,isrun,btime,etime,cancel,password,program,result,issyn) values (myseq.nextval,'','wx_chk.f','0',TO_DATE('"+tdate+"','yyyy-MM-dd hh24:mi:ss'),TO_DATE('"+ edate[0] +"','yyyy-MM-dd hh24:mi:ss'),0,'','11','"+result[0]+"','1')").select();
                });

                return  "Asysn";
            }
            else{
                Dbutil.R r1 = null;
                Dbutil.Qp qp1 = null;
                try {
                    qp1 = new Dbutil.Qp();
                    Object o = qp1.setUsrpwd("pigpos", "pigpos").func(1,"wx_chk.f");
                } finally {
                    qp1.closeConn();
                }
                afterProcess();
                //更新记录
            }

        } finally {
            if (qp[0]!=null){
                qp[0].closeConn();
            }

        }

        return "";
    }


    public abstract  String afterProcess();


    void  exec(String pd){

        ProgExecContr pec=new  ProgExecContr(){


            @Override
            public String afterProcess(){
                return "";
            }

        };

    }
}
